﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using lcmyjobs.Model;
using System.Xml;

namespace SendEmail
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            SendEmail();
        }

        private void SendEmail()
        {
            string receives = "";
            SendEmail.enterprise_basic eb = new SendEmail.enterprise_basic();


            DataSet ds_eb = eb.GetList(" issubsrciption = 1");
            DataSet ds_u = eb.GetListUserEmail("issubscription = 1");

            if (ds_eb.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds_eb.Tables[0].Rows)
                {
                    if (Convert.ToBoolean(row["issubsrciption"]))
                    {
                        receives += row["email"].ToString() + ",";
                    }
                }
            }
            if (ds_u.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds_u.Tables[0].Rows)
                {
                    if (Convert.ToBoolean(row["issubscription"]))
                    {
                        receives += row["email"].ToString() + ",";
                    }
                }
            }

            if (receives.Length > 0)
            {
                receives = receives.Substring(0, receives.LastIndexOf(","));

                email em = ReadXml(receives);

                if (em != null)
                {

                    lcmyjobs.BLL.SendEmail sm = new lcmyjobs.BLL.SendEmail(em);

                    sm.Send();
                }
            }
           

        }

        private email ReadXml(string receives)
        {
            email em = new email();
               
            try
            {
                XmlReader read = new XmlTextReader("email.xml");

                XmlDocument doc = new XmlDocument();

                doc.Load(read);

                XmlNode root = doc.DocumentElement;

                em.sendEmailAdd = root.SelectSingleNode("sendEmailAdd").InnerText;
                em.sendEmailPwd = root.SelectSingleNode("sendEmailPwd").InnerText;
                em.serverhost = root.SelectSingleNode("emailHost").InnerText;
                em.port = Convert.ToInt32(root.SelectSingleNode("emailHostPort").InnerText);
                em.receiveEmailAdd = receives;
                em.content = root.SelectSingleNode("emailContent").InnerText;
                em.title = root.SelectSingleNode("emailTitle").InnerText;

                return em;
            }
            catch (Exception)
            {
                em = null;
                MessageBox.Show("系统出错，请确认email.xml文件是否在程序文件夹中");
                
            }
            return em;
               
        }
    }
}
